package com.apollow.oschina.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.apollow.oschina.R;
import com.apollow.oschina.util.ImageUtils;
import com.apollow.oschina.widget.SelfDialog;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Dragon on 2017/1/14.
 * 我的资料界面
 */
public class UserInfoActivity extends AppCompatActivity {
    @BindView(R.id.ll_back)
    LinearLayout mLlBack;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.civ_header)
    CircleImageView mCivHeader;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_time)
    TextView mTvTime;
    @BindView(R.id.tv_address)
    TextView mTvAddress;
    @BindView(R.id.tv_develop)
    TextView mTvDevelop;
    @BindView(R.id.tv_special)
    TextView mTvSpecial;

    private SelfDialog mSelfDialog;
    private static final int PHOTO_ALBUM = 1;
    private Uri mImageUri;
    private static final int Cut_PHOTO = 2;
    private static final int SHOW_PHOTO = 3;
    private static final int SHOW_PHOTO_ALBUM = 4;
    private Uri uri;
    private Bitmap mBitmap;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        ButterKnife.bind(this);
        mTvTitle.setText("我的资料");
        initData();
        initEvent();
    }

    private void initEvent() {
        //头像点击监听
        mCivHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSelfDialog = new SelfDialog(UserInfoActivity.this);
                mSelfDialog.setNoOnclickListener("取消", new SelfDialog.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        mSelfDialog.dismiss();
                    }
                });
                //更换头像监听
                mSelfDialog.setChangeOnclickListener(new SelfDialog.onChangeOnclickListener() {
                    @Override
                    public void onChangeClick() {
                        // 创建File对象,用于存储选择的照片
                        File outputImage = new File(Environment.getExternalStorageDirectory(), "head.jpg");
                        try {
                            if (outputImage.exists()) {
                                outputImage.delete();
                            }
                            outputImage.createNewFile();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        mImageUri = Uri.fromFile(outputImage);
                        Intent intent = new Intent("android.intent.action.GET_CONTENT");
                        intent.setType("image/*");
                        //允许裁剪
                        intent.putExtra("crop", true);
                        //允许缩放
                        intent.putExtra("scale", true);
                        //图片的输出位置
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
                        startActivityForResult(intent, PHOTO_ALBUM);
                    }
                });

                //查看大头像监听
                mSelfDialog.setLookBigOnclickListener(new SelfDialog.onLookBigOnclickListener() {
                    @Override
                    public void onLookBigClick() {
                        Intent intent = new Intent(UserInfoActivity.this, LookBigActivity.class);
                        startActivity(intent);
                    }
                });
                mSelfDialog.show();
            }
        });
    }

    private void initData() {
        Bitmap bitmap = ImageUtils.getBitmap(this, "head.jpg");
        if (bitmap != null) {
            mCivHeader.setImageBitmap(bitmap);
        }
    }

    @OnClick(R.id.ll_back)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case Cut_PHOTO:
                if (resultCode == RESULT_OK) {
                    Intent intent = new Intent("com.android.camera.action.CROP");
                    intent.setDataAndType(mImageUri, "image/*");
                    intent.putExtra("scale", true);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
                    // 启动裁剪
                    startActivityForResult(intent, SHOW_PHOTO);
                }
                break;
            case SHOW_PHOTO:
                if (resultCode == RESULT_OK) {
                    try {
                        mBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(mImageUri));
                        // 将裁剪后的照片显示出来
                        mCivHeader.setImageBitmap(mBitmap);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;
            case PHOTO_ALBUM:
                if (resultCode == RESULT_OK) {
                    uri = data.getData();
                    Intent intent = new Intent("com.android.camera.action.CROP");
                    intent.setDataAndType(uri, "image/*");
                    intent.putExtra("scale", true);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
                    // 启动裁剪
                    startActivityForResult(intent, SHOW_PHOTO_ALBUM);
                }
                break;
            case SHOW_PHOTO_ALBUM:
                if (resultCode == RESULT_OK) {
                    try {
                        mBitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(mImageUri));
                        // 将裁剪后的照片显示出来
                        mCivHeader.setImageBitmap(mBitmap);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                    try {
                        //保存图片
                        ImageUtils.saveImage(this, "head.jpg", mBitmap);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                break;
            default:
                break;
        }
    }
}
